﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using IFViewDetail.KuaiQi;
using IFClientDLL;
using IFServerDLL.Model;

namespace IFViewDetail.Ctl.Demo
{
    public partial class CtlUserOrder : DevExpress.XtraEditors.XtraUserControl
    {
        public CtlUserOrder()
        {
            InitializeComponent();
        }
        private ITryIF _tryif;
        public void Init(ITryIF tryIF)
        {
            _tryif = tryIF;
            _tryif.DataServer.OnReceiveDBData += new Action<IFServerDLL.DB.MyDBData>(Tryif_OnReceiveData);
            cmbOpenOrClose.Properties.Items.AddEnum(typeof(IFServerDLL.Model.MyEnumOpenClose));
           
        }

        void Tryif_OnReceiveData(IFServerDLL.DB.MyDBData obj)
        {
            txtInstrment.Text = obj.Instrument;
            if (txtPrice.EditValue == null)
            {
                txtPrice.EditValue = obj.NewPrice;
                txtPrice.Properties.Mask.EditMask = MyVariety.GetVarietyByInstrument(obj.Instrument).PriceFormat;
                txtPrice.Properties.Increment = MyVariety.GetVarietyByInstrument(obj.Instrument).OneChangedPrice;
                cmbOpenOrClose.SelectedIndex = 0;
            }
            txtVolume.EditValue = 1;
            _obj = obj;
        }
        IFServerDLL.DB.MyDBData _obj;
       

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (cmbOpenOrClose.EditValue == null)
                return;
            if (txtPrice.Value == 0)
                return;
            _tryif.AddOrder(new IFServerDLL.Model.UserOrdBase() { BuyOrSell = rdgBuyOrSell.SelectedIndex == 0 ? MyEnumDirection.Buy : MyEnumDirection.Sell, OpenOrClose = (MyEnumOpenClose)cmbOpenOrClose.EditValue , Instrument = txtInstrment.Text, UserCreateOrdTime = DateTime.Now, Volume = (int)txtVolume.Value, Price = txtPrice.Value});
        }

        private void rdgBuyOrSell_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_obj != null)
            {
                txtPrice.EditValue = _obj.NewPrice;
            }
        }

        private void cmbOpenOrClose_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_obj != null)
            {
                txtPrice.EditValue = _obj.NewPrice;
            }
        }
    }
}
